﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.ETL.Domain
{
    class TaskKeeper
    {
        private static object s_lockObj = new object();

        private static Hashtable s_hashtable = new Hashtable();

        /// <summary>
        /// 判断任务是否存在
        /// </summary>
        /// <param name="taskKey"></param>
        /// <returns></returns>
        public static bool Exist(string taskKey)
        {
            if(s_hashtable.ContainsKey(taskKey))
            {
                return true;
            }
            return false;
        }

        public static void Register(string taskKey)
        {
            if (!s_hashtable.ContainsKey(taskKey))
            {
                lock(s_lockObj)
                {
                    if (!s_hashtable.ContainsKey(taskKey))
                    {
                        s_hashtable.Add(taskKey, 1);
                    }
                }
            }
        }

        public static void Unregister(string taskKey)
        {
            if (s_hashtable.ContainsKey(taskKey))
            {
                lock (s_lockObj)
                {
                    if (s_hashtable.ContainsKey(taskKey))
                    {
                        s_hashtable.Remove(taskKey);
                    }
                }
            }
        }
    }
}
